package cn.com.geovis.demo.controller;

import cn.com.geovis.demo.enums.EnumCode;
import cn.com.geovis.demo.model.DbSource;
import cn.com.geovis.demo.service.DbSourceService;
import cn.com.geovis.demo.utils.ResultUtil;
import cn.hutool.json.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jakarta.validation.Valid;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@Validated
@RequestMapping("/dbsource")
@Api(value = "DbSourceApi")
public class DbSourceController {
    @Resource
    private DbSourceService dbSourceService;

    @GetMapping("/hello")
    @ApiOperation(value = "简单 Get 示例", notes = "")
    public String hello(){
        return ResultUtil.result(EnumCode.OK.getValue().toString(), EnumCode.OK.getText(), "Hello Kubesphere!");
    }

    @GetMapping("/getDbSource")
    @ApiOperation(value = "查询数据源列表", notes = "")
    public Object getDbSource(DbSource dbSource){
        List<DbSource> list = dbSourceService.selectDbSource(dbSource);
        JSONObject jsonObject = new JSONObject();
        jsonObject.putOnce("dbSourceList", list);
        return ResultUtil.result(EnumCode.OK.getValue().toString(), EnumCode.OK.getText(), jsonObject);
    }

    @PostMapping("/addDbSource")
    @ApiOperation(value = "添加数据源列表", notes = "")
    public Object addDbSource(@Valid @RequestBody DbSource dbSource){
        return dbSourceService.addDbSource(dbSource);
    }

    @DeleteMapping("/deleteDbSource")
    @ApiOperation(value = "删除数据源列表", notes = "")
    public Object deleteDbSource(Integer []ids){
        return dbSourceService.deleteDbSource(ids);
    }

    @PostMapping("/updateDbSource")
    @ApiOperation(value = "更新数据源列表", notes = "")
    public Object updateDbSource(@RequestBody DbSource dbSource){
        return dbSourceService.updateDbSource(dbSource);
    }
}
